package cn.mastercom.gui.hdfsExplorer.views;

import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;
import java.io.Writer;
import java.util.Scanner;

import javax.swing.JTextArea;

import lombok.extern.log4j.Log4j;
import org.apache.log4j.Appender;
import org.apache.log4j.Logger;
import org.apache.log4j.WriterAppender;

@SuppressWarnings("serial")
@Log4j
public class LogArea extends JTextArea implements Runnable {


   private transient PipedReader reader;
   
   LogArea() throws IOException {
	   super();
	   Logger root = Logger.getRootLogger();
       // 获取子记录器的输出源 
       Appender appender = root.getAppender("console");
       // 定义一个未连接的输入流管道
       reader = new PipedReader();
       // 定义一个已连接的输出流管理，并连接到reader
       Writer writer = new PipedWriter(reader);
       // 设置 appender 输出流
       ((WriterAppender) appender).setWriter(writer);
       
       // 开始输出日志
       new Thread(this).start();
   }

   @Override
   public void run() {
       // 不间断地扫描输入流
       @SuppressWarnings("resource") Scanner scanner = new Scanner(reader);
       // 将扫描到的字符流输出到指定的JTextArea组件
       while (scanner.hasNextLine()) {
           try {
               //睡眠
               Thread.sleep(100);
               String line = scanner.nextLine();
               this.append(line);
               this.append("\n");
              } catch (Exception e) {
               log.error(e);
           }
       }
   }
}